home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10196 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  84 lines

  1. Path: newsfeed.direct.ca!usenet
  2. From: qjackson@direct.ca
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Pointers to Arrays (or not)
  5. Date: Wed, 06 Mar 1996 15:40:23 GMT
  6. Organization: Parsepolis Software
  7. Message-ID: <4hkbg9$cja@aphex.direct.ca>
  8. Reply-To: qjackson@direct.ca
  9. NNTP-Posting-Host: 204.174.249.114
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Hello:
  13.  
  14. I've come across something that I'm wondering about.
  15.  
  16. I have a class, Foo, with a member that is a pointer to Bar, where Bar
  17. can be either a single object of Bar, or an array of Bar, depending on
  18. other semantics:
  19.  
  20.  
  21. class Foo
  22. {
  23.  
  24.     Bar* pBar;
  25.     int barTab; // how many Bar's are pointed to be pBar
  26.  
  27.     ~Foo (void);
  28.  
  29. };
  30.  
  31. I would have thought that I could kill pBar this way:
  32.  
  33.     delete [] pBar;
  34.  
  35. but that crashes if pBar was instantiated thus:
  36.  
  37.     pBar = new Bar;
  38.  
  39. So, my destructor has this:
  40.  
  41.  
  42. Foo::~Foo (void)
  43. {
  44.  
  45.     switch (barTab)
  46.     {
  47.         case 0:
  48.             break;
  49.  
  50.         case 1:
  51.             delete pBar;
  52.             break;
  53.  
  54.         default:
  55.             delete [] pBar;
  56.             break;
  57.  
  58.     }
  59.  
  60. }
  61.  
  62. Without this, under VC++ 1.52, the system is prone to crash.
  63.  
  64. What am I missing?
  65.  
  66.  
  67. Thanks in advance,
  68.  
  69.  
  70.  
  71.  
  72.  
  73.     
  74.  
  75.  
  76.  
  77. --                           
  78.                            | 
  79.     Parsepolis Software    |   Quinn Tyler Jackson
  80.         "ParseCity"        |     (aka 'Jamshid')
  81. >--------------------------|   qjackson@direct.ca
  82.                            |---------------------->
  83.  
  84.